home *** CD-ROM | disk | FTP | other *** search
- /*
- * SFskyedit - Star Fighter 3000 sky colours editor
- * Menu attached to sky window (all levels)
- * Copyright (C) 2001 Chris Bazley
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public Licence as published by
- * the Free Software Foundation; either version 2 of the Licence, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public Licence for more details.
- *
- * You should have received a copy of the GNU General Public Licence
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- */
-
- /* ANSI library files */
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- #include <stdbool.h>
-
- /* RISC OS library files */
- #include "kernel.h"
- #include "wimp.h"
- #include "toolbox.h"
- #include "event.h"
- #include "wimplib.h"
- #include "menu.h"
-
- /* My library files */
- #include "err.h"
- #include "msgtrans.h"
- #include "Macros.h"
- #include "Pal256.h"
- #include "FilePerc.h"
-
- /* Local headers */
- #include "EditSky.h"
- #include "SFSFileInfo.h"
- #include "SFSSaveBox.h"
- #include "Interpolate.h"
- #include "Insert.h"
- #include "Back-end.h"
- #include "DCS_dialogue.h"
- #include "Utils.h"
- #include "Main.h"
- #include "Menus.h"
-
- /* Menu entries */
- #define SKYMENU_FILE 0x01
- #define SKYMENU_EDIT 0x02
- #define SKYMENU_COLS 0x0a
- #define SKYMENU_TRAP 0x09
- #define SKYMENU_PREVIEW 0x05
- #define SKYMENU_HELP 0x08
-
- #define FILEMENU_INFO 0x01
- #define FILEMENU_SAVE 0x02
- #define FILEMENU_DISCARD 0x00
-
- #define EDITMENU_INSERTNEW 0x0b
- #define EDITMENU_CUT 0x08
- #define EDITMENU_COPY 0x07
- #define EDITMENU_PASTE 0x09
- #define EDITMENU_DELETE 0x06
- #define EDITMENU_SELALL 0x0a
- #define EDITMENU_CLEARSEL 0x03
-
- #define COLSMENU_SETCOL 0x01
- #define COLSMENU_GRADIENT 0x05
- #define COLSMENU_SMOOTH 0x04
-
- ObjectId SkyMenu_sharedid = NULL_ObjectId, EditMenu_sharedid = NULL_ObjectId, ColsMenu_sharedid = NULL_ObjectId;
-
- /* ----------------------------------------------------------------------- */
- /* Function prototypes */
-
- static ToolboxEventHandler _SkyMenu_showhandler, _SkyMenu_selectionhandler, _FileMenu_selectionhandler, _EditMenu_selectionhandler, _EditMenu_showhandler, _ColsMenu_colourselhandler, _ColsMenu_selectionhandler, _ColsMenu_showhandler;
-
- /* ----------------------------------------------------------------------- */
- /* Public functions */
-
- void SkyMenu_initialise(ObjectId id)
- {
- SkyMenu_sharedid = id;
-
- /* Install handlers */
- EF(event_register_toolbox_handler(id, Menu_Selection, _SkyMenu_selectionhandler, NULL));
- EF(event_register_toolbox_handler(id, Menu_AboutToBeShown, _SkyMenu_showhandler, NULL));
- }
-
- /* ----------------------------------------------------------------------- */
-
- void FileMenu_initialise(ObjectId id)
- {
- /* Install handlers */
- EF(event_register_toolbox_handler(id, Menu_Selection, _FileMenu_selectionhandler, NULL));
- }
-
- /* ----------------------------------------------------------------------- */
-
- void EditMenu_initialise(ObjectId id)
- {
- EditMenu_sharedid = id;
-
- /* Install handlers */
- EF(event_register_toolbox_handler(id, Menu_Selection, _EditMenu_selectionhandler, NULL));
- EF(event_register_toolbox_handler(id, Menu_AboutToBeShown, _EditMenu_showhandler, NULL));
- }
-
- /* ----------------------------------------------------------------------- */
-
- void ColsMenu_initialise(ObjectId id)
- {
- ColsMenu_sharedid = id;
-
- /* Install handlers */
- EF(event_register_toolbox_handler(id, Menu_Selection, _ColsMenu_selectionhandler, NULL));
- EF(event_register_toolbox_handler(id, Menu_AboutToBeShown, _ColsMenu_showhandler, NULL));
- EF(event_register_toolbox_handler(pal256_sharedid, Pal256_ColourSelected, _ColsMenu_colourselhandler, NULL));
- }
-
- /* ----------------------------------------------------------------------- */
-
- //void SkyMenu_update(ViewData *view_data)
- //{
- // RE(menu_set_tick(0, SkyMenu_sharedid, SKYMENU_TRAP, (int)view_data->trap_caret))
- //}
-
- /* ----------------------------------------------------------------------- */
-
- void EditMenu_update(ViewData *view_data)
- {
- /* If full selection then prevent select all */
- RE(menu_set_fade(0, EditMenu_sharedid, EDITMENU_SELALL, (view_data->selection_exists && view_data->selection_start == 0 && view_data->selection_end == 62)))
-
- /* Prevent operations on selection if none */
- RE(menu_set_fade(0, EditMenu_sharedid, EDITMENU_CLEARSEL, !view_data->selection_exists))
- RE(menu_set_fade(0, EditMenu_sharedid, EDITMENU_CUT, !view_data->selection_exists))
- RE(menu_set_fade(0, EditMenu_sharedid, EDITMENU_COPY, !view_data->selection_exists))
- RE(menu_set_fade(0, EditMenu_sharedid, EDITMENU_DELETE, !view_data->selection_exists))
-
- /* Prevent insertion at caret if none */
- RE(menu_set_fade(0, EditMenu_sharedid, EDITMENU_INSERTNEW, view_data->selection_exists))
-
- /* Prevent paste if no clipboard contents */
- RE(menu_set_fade(0, EditMenu_sharedid, EDITMENU_PASTE, clipboard == NULL))
- }
-
-
- /* ----------------------------------------------------------------------- */
-
- //void ColsMenu_update(ViewData *view_data)
- //{
- // /* Prevent operations if none selected (or not enough bands) */
- // RE(menu_set_fade(0, ColsMenu_sharedid, COLSMENU_SETCOL, !view_data->selection_exists))
- // RE(menu_set_fade(0, ColsMenu_sharedid, COLSMENU_GRADIENT, !view_data->selection_exists || ((view_data->selection_end+1) - view_data->selection_start) < 2))
- // RE(menu_set_fade(0, ColsMenu_sharedid, COLSMENU_SMOOTH, !view_data->selection_exists || ((view_data->selection_end+1) - view_data->selection_start) < 3))
- //}
-
- /* ----------------------------------------------------------------------- */
- /* Private functions */
-
- static int _SkyMenu_showhandler(int event_code, ToolboxEvent *event, IdBlock *id_block, void *handle)
- {
- ViewData *view_data;
- E_RETV(toolbox_get_client_handle(0, id_block->ancestor_id, (void **)&view_data), 1)
-
- //SkyMenu_update(view_data);
- RE(menu_set_tick(0, SkyMenu_sharedid, SKYMENU_TRAP, (int)view_data->trap_caret))
-
- return 0; /* claim event */
- }
-
- /* ----------------------------------------------------------------------- */
-
- static int _SkyMenu_selectionhandler(int event_code, ToolboxEvent *event, IdBlock *id_block, void *handle)
- {
- ViewData *view_data;
- E_RETV(toolbox_get_client_handle(0, id_block->ancestor_id, (void **)&view_data), 0)
-
- switch(id_block->self_component) {
-
- case SKYMENU_PREVIEW:
- EditSky_preview(view_data);
- return 1; /* claim event */
-
- case SKYMENU_TRAP:
- view_data->trap_caret = !view_data->trap_caret;
- def_trap_caret = view_data->trap_caret;
- RE(menu_set_tick(0, SkyMenu_sharedid, SKYMENU_TRAP, (int)view_data->trap_caret))
- return 1; /* claim event */
-
- default:
- return 0; /* event not handled */
- }
- }
-
- /* ----------------------------------------------------------------------- */
-
- static int _FileMenu_selectionhandler(int event_code, ToolboxEvent *event, IdBlock *id_block, void *handle)
- {
- ViewData *view_data;
- E_RETV(toolbox_get_client_handle(0, id_block->ancestor_id, (void **)&view_data), 0)
-
- switch(id_block->self_component) {
- case FILEMENU_INFO:
- RE(toolbox_show_object(Toolbox_ShowObject_AsMenu, fileinfo_sharedid, Toolbox_ShowObject_AtPointer, NULL, id_block->self_id, id_block->self_component))
- return 1; /* claim event */
-
- case FILEMENU_SAVE:
- if(strchr(view_data->last_savepath, (int)'.') == NULL) {
- /* Must open savebox first */
- RE(toolbox_show_object(Toolbox_ShowObject_AsMenu, savebox_sharedid, Toolbox_ShowObject_AtPointer, NULL, id_block->self_id, id_block->self_component))
- }
- else {
- /* Save file immediately */
- _kernel_oserror *err = perc_operation(FILEPERC_OP_COMP, view_data->last_savepath, FILETYPE_SKYCOLS, (flex_ptr)&view_data->sky);
- if(err != NULL)
- /* Saving error */
- err_report(err->errnum, msgs_lookup_sub1("SaveFail", err->errmess));
- else
- EditSky_newfile(view_data, view_data->last_savepath, 1);
- }
- return 1; /* claim event */
-
- case FILEMENU_DISCARD:
- if(view_data->changed_since_save) {
- /* Unsaved data - prompt user for action */
- dcs_openparent = false;
- RE(open_topleftofwin(Toolbox_ShowObject_AsMenu, dcs_sharedid, id_block->ancestor_id, id_block->self_id, id_block->self_component))
- }
- else {
- /* Close immediately */
- RE(toolbox_delete_object(0, id_block->ancestor_id))
- }
- return 1; /* claim event */
- }
-
- return 0; /* event not handled */
- }
-
- /* ----------------------------------------------------------------------- */
-
- static int _EditMenu_showhandler(int event_code, ToolboxEvent *event, IdBlock *id_block, void *handle)
- {
- ViewData *view_data;
- E_RETV(toolbox_get_client_handle(0, id_block->ancestor_id, (void **)&view_data), 1)
- EditMenu_update(view_data);
- return 1; /* claim event */
- }
-
- /* ----------------------------------------------------------------------- */
-
- static int _EditMenu_selectionhandler(int event_code, ToolboxEvent *event, IdBlock *id_block, void *handle)
- {
- ViewData *view_data;
- E_RETV(toolbox_get_client_handle(0, id_block->ancestor_id, (void **)&view_data), 0)
-
- switch(id_block->self_component) {
- case EDITMENU_PASTE:
- /* Insert rows from clipboard */
- EditSky_paste(view_data);
- break;
-
- case EDITMENU_INSERTNEW:
- if(view_data->selection_exists) {
- _kernel_oswrch(7); /* beep */
- return 1;
- }
- RE(wimp_create_menu(CloseMenu,0,0))
- RE(toolbox_show_object(0, Insert_sharedid, Toolbox_ShowObject_AtPointer, NULL, id_block->self_id, id_block->self_component))
- return 1; /* don't update menu */
-
- case EDITMENU_COPY:
- if(!view_data->selection_exists) {
- _kernel_oswrch(7); /* beep */
- return 1;
- }
- copy_to_clipboard(&view_data->sky, view_data->selection_start, view_data->selection_end);
- break;
-
- case EDITMENU_CUT:
- if(!view_data->selection_exists) {
- _kernel_oswrch(7); /* beep */
- return 1;
- }
- if(!copy_to_clipboard(&view_data->sky, view_data->selection_start, view_data->selection_end))
- return 1;
- EditSky_remove_sel(view_data);
- break;
-
- case EDITMENU_DELETE:
- if(!view_data->selection_exists) {
- _kernel_oswrch(7); /* beep */
- return 1;
- }
- EditSky_remove_sel(view_data);
- break;
-
- case EDITMENU_SELALL:
- EditSky_changeselection(view_data, 0, 62);
- break;
-
- case EDITMENU_CLEARSEL:
- EditSky_clearselection(view_data);
- break;
-
- default:
- return 0; /* event not handled */
- }
-
- /* Update menu state (selection and/or clipboard status may have changed) */
- EditMenu_update(view_data);
-
- return 1; /* claim event */
- }
-
- /* ----------------------------------------------------------------------- */
-
- static int _ColsMenu_showhandler(int event_code, ToolboxEvent *event, IdBlock *id_block, void *handle)
- {
- ViewData *view_data;
- E_RETV(toolbox_get_client_handle(0, id_block->ancestor_id, (void **)&view_data), 1)
- //ColsMenu_update(view_data);
-
- /* Prevent operations if none selected (or not enough bands) */
- RE(menu_set_fade(0, ColsMenu_sharedid, COLSMENU_SETCOL, !view_data->selection_exists))
- RE(menu_set_fade(0, ColsMenu_sharedid, COLSMENU_GRADIENT, !view_data->selection_exists || ((view_data->selection_end+1) - view_data->selection_start) < 2))
- RE(menu_set_fade(0, ColsMenu_sharedid, COLSMENU_SMOOTH, !view_data->selection_exists || ((view_data->selection_end+1) - view_data->selection_start) < 3))
-
- return 1; /* claim event */
- }
-
- /* ----------------------------------------------------------------------- */
-
- static int _ColsMenu_selectionhandler(int event_code, ToolboxEvent *event, IdBlock *id_block, void *handle)
- {
- ViewData *view_data;
- E_RETV(toolbox_get_client_handle(0, id_block->ancestor_id, (void **)&view_data), 0)
-
- switch(id_block->self_component) {
- case COLSMENU_SETCOL:
- if(!view_data->selection_exists) {
- _kernel_oswrch(7); /* beep */
- return 1; /* ignore if no selection at present */
- }
- if(!E(Pal256_set_colour(pal256_sharedid, get_shade(&view_data->sky, view_data->selection_start))))
- RE(toolbox_show_object(Toolbox_ShowObject_AsMenu, pal256_sharedid, Toolbox_ShowObject_AtPointer, NULL, id_block->self_id, id_block->self_component))
- return 1; /* claim event */
-
- case COLSMENU_SMOOTH:
- if(!view_data->selection_exists || ((view_data->selection_end+1) - view_data->selection_start) < 3) {
- _kernel_oswrch(7); /* beep */
- return 1; /* ignore if no selection at present */
- }
- smooth_section(&view_data->sky, view_data->selection_start, view_data->selection_end);
- EditSky_rowsupdated(view_data, view_data->selection_start, view_data->selection_end);
- EditSky_markaschanged(view_data);
- return 1; /* claim event */
-
- case COLSMENU_GRADIENT:
- if(!view_data->selection_exists || ((view_data->selection_end+1) - view_data->selection_start) < 2) {
- _kernel_oswrch(7); /* beep */
- return 1; /* ignore if no selection at present */
- }
- RE(wimp_create_menu(CloseMenu,0,0))
- RE(toolbox_show_object(0, Interpolate_sharedid, Toolbox_ShowObject_AtPointer, NULL, id_block->self_id, id_block->self_component))
- return 1; /* claim event */
- }
- return 0; /* event not handled */
- }
-
- /* ----------------------------------------------------------------------- */
-
- static int _ColsMenu_colourselhandler(int event_code, ToolboxEvent *event, IdBlock *id_block, void *handle)
- {
- /* Colour selected - fill selected skyfile bands */
- if(id_block->parent_id != ColsMenu_sharedid)
- return 0; /* none of our business */
-
- ViewData *view_data;
- E_RETV(toolbox_get_client_handle(0, id_block->ancestor_id, (void **)&view_data), 1)
- plain_overwrite(&view_data->sky, view_data->selection_start, view_data->selection_end, ((Pal256ColourSelectedEvent *)event)->colour_number);
- EditSky_rowsupdated(view_data, view_data->selection_start, view_data->selection_end);
- EditSky_markaschanged(view_data);
-
- return 1; /* claim event */
- }
-